package com.module.msg.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.module.app.base.BaseListFragment
import com.module.app.bean.ListViewBean
import com.module.app.ext.loginFlow
import com.module.base.base.adapter.BaseAdapter
import com.module.base.base.adapter.BaseNewAdapter
import com.module.base.utils.CollectionUtil
import com.module.base.utils.DisplayUtils
import com.module.base.utils.ScrollUtils
import com.module.frame.ext.observe
import com.module.msg.R
import com.module.msg.adapter.holder.MsgHolder
import com.module.msg.bean.MsgBean
import com.module.msg.databinding.MsgFragMsgMyBinding
import com.module.msg.ext.myMsgFlow
import com.module.msg.model.MyMsgViewModel
import com.module.msg.utils.startMsg

class MyMsgFragment : BaseListFragment<MyMsgViewModel, MsgFragMsgMyBinding, BaseAdapter>() {

    private val headerView: View by lazy {
        LayoutInflater.from(context).inflate(R.layout.app_header_empty, null, false)
    }

    override fun bingViewModel() {
        mDatabind.vm = mViewModel
    }

    override fun createAdapter(): BaseAdapter {
        return object : BaseAdapter() {
            override fun onCreateDefaultViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): MsgHolder {
                return MsgHolder(parent)
            }
        }
    }

    override fun onLazyLoad() {
        reload()
    }

    override fun initView() {
        super.initView()
        ScrollUtils(context, mDatabind.ivAdd, recyclerview)
        headerView.setPadding(
            headerView.paddingLeft,
            DisplayUtils.dip2px(5f),
            headerView.paddingRight,
            headerView.paddingBottom
        )
    }

    override fun initListener() {
        super.initListener()
        //登录监听
        observe(loginFlow) {
            refresh()
        }
        observe(myMsgFlow) {
            refresh()
        }
        adapter.setOnItemClickListener(BaseNewAdapter.OnItemClickListener<MsgHolder?, MsgBean?> { _, bean ->
            context.startMsg(bean)
        })
        mDatabind.ivAdd.setOnClickListener {
            context.startMsg(null)
        }
    }


    override fun onListLoadPage(
        isPull: Boolean,
        isRefresh: Boolean,
        page_no: Int,
        page_size: Int
    ) {
        mViewModel.getData()
    }

    override fun setListData(bean: ListViewBean) {
        super.setListData(bean)
        if (bean.isRefresh && !CollectionUtil.isEmptyOrNull(bean.list)) {
            adapter.setHeaderView(headerView)
        }
    }
}